<template>
	<view class="search">
		<u-navbar :is-back="false">
			<view class="header">
				<view class="inputDiv">
					<input class="input" v-model="mobile" placeholder="请输入手机号" />
					<image class="search-icon" @click="searchUser" src="../../../static/image/search/search@2x.png"></image>
				</view>
				<view class="cancel" @click="back">取消</view>
			</view>
		</u-navbar>

		<view class="list">
			<view class="title" v-if="list.length > 0">用户</view>
			<view class="item u-border-bottom" v-for="(ite,i) of list" :key="i">
				<view class="left">
					<image class="head" :src="!ite.avatar ? '../../../static/logo.png' : ite.avatar"></image>
					<view class="name">{{ite.nickname}}</view>
				</view>
				<view class="right">
					<view class="sendMsg" v-if="false">发消息</view>
					<view class="add" @click="addFriend(ite.id)">加好友</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { mapState, mapActions } from 'vuex'
export default {
	data() {
		return {
			list: [],
			mobile: ''
		};
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		async searchUser() {
			this.list = []
			if(!this.$u.test.mobile(this.mobile)) {
				this.$u.toast("手机号错误")
				return
			}
			let res = await this.$u.api.searchUser({mobile:this.mobile})
			if (res.status_code == 200) {
				this.list.push(res.data)
			}
		},
		async addFriend(dstId) {
			const id = this.userInfo.id
			if(id == dstId) {
				this.$u.toast("不能加自己为好友")
				return
			}
			let res = await this.$u.api.addFriend({dstId:dstId})
			if (res != '') {
				this.$u.toast(res.message)
			}
			setTimeout(() => {
				var pages = getCurrentPages()
				var prevPage = pages[pages.length - 2]
				prevPage.getFriendsList()
				this.back()
			}, 1000);
		},
		back() {
			this.$u.route({
				type: 'back',
				animationType: 'pop-out'
			})
		}
	}
};
</script>

<style lang="scss" scoped>
.search {
	.header {
		display: flex;
		align-items: center;
		flex: 1;
		justify-content: space-between;
		padding: 0 32rpx;

		.inputDiv {
			display: flex;
			align-items: center;
			background: #f3f4f6;
			border-radius: 10rpx;
			flex: 1;
			padding: 10rpx 12rpx;

			.input {
				flex: 1;
				font-size: 28rpx;
			}

			.search-icon {
				width: 40rpx;
				height: 40rpx;
			}
		}

		.cancel {
			margin-left: 30rpx;
			font-size: 28rpx;
			color: #272832;
			letter-spacing: -0.96rpx;
			line-height: 40rpx;
		}
	}

	.list{
		.title {
			padding: 34rpx 32rpx 0;
			font-size: 44rpx;
			color: #272832;
			letter-spacing: -1.5rpx;
			line-height: 60rpx;
		}
		.item {
			padding: 20rpx 32rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.left{
				display: flex;
				align-items: center;
				.head{
					width: 80rpx;
					height: 80rpx;
					border-radius: 20rpx;
				}
				.name {
					margin-left: 32rpx;
					font-size: 36rpx;
					color: #272832;
					letter-spacing: -1.24rpx;
				}
			}
			.right {
				.sendMsg {
					background: #FFE431;
					border-radius: 24rpx;
					padding: 8rpx 24rpx;
					font-size: 24rpx;
					color: #272832;
					letter-spacing: -0.82rpx;
					line-height: 34rpx;
				}
				
				.add{
					background: rgba(74,170,255,0.10);
					border-radius: 24rpx;
					padding: 8rpx 24rpx;
					font-size: 24rpx;
					color: #4AAAFF;
					letter-spacing: -0.82rpx;
					line-height: 34rpx;
				}
			}
		}
	}
}
</style>